<?php
class comment extends shopAdminBase {
    function __construct() {
        parent::__construct();
        $this->cms->setPageTitle("评价管理");
        $this->cms->setControlFile("shop/comment/comment_control.json");
    }

    function indexTask($page=1,$id,$name,$username,$real_name){
        $this->cms->setActionTitle("所有评价");
        $user_info=$this->user->getLoginInfo();
        $shop=ShopMode::init();
        $shop_id=$shop->getShopId($user_info['id']);
        $goods=GoodsMode::init();
        $comment=CommentModule::init("goods_comment",'user_id','gopen_id');
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $username=isset($username)? trim($username):null;
        $real_name=isset($real_name)? trim($real_name):null;
        if(isset($real_name)&&!empty($real_name)){
            $user_id=UserMode::init()->getInfoByRealName($real_name)['user_id'];
            $user_id=isset($user_id)?$user_id:-1;
        }else if(isset($username)&&!empty($username)){
            $user_id=UserMode::init()->getIdByUsername($username);
            $user_id=isset($user_id)?$user_id:-1;
        }else{
            $user_id = null;
        }
        $name=isset($name)? trim($name):null;
        list($goods_info,$goods_total)=$goods->getList($shop_id,null,null,$name,
            null, false,$countOfPage,$offset);
        $r['goods_info']=is_array($goods_info)?$goods_info:array();
        $total=0;
        $i=0;
        $r['goods_comment']=array();
        foreach($r['goods_info'] as $v){
            list($r['comment'],$comment_total)=$comment->getList($user_id,$v);
            $r['comment']=is_array($r['comment'])?$r['comment']:array();
            foreach($r['comment'] as $k){
                if(!empty($k)){
                    $r['goods_comment'][$i]=$k;
                    $i++;
                }
            }
            $total=!empty($comment_total)?$total+$comment_total:$total;
        }
        if(isset($id)&&!empty($id)){
            $r['goods_comment']=array();
            $r['goods_comment'][0]=$comment->getDetail($id);
            $total = 1;
        }
        $url=WebRouter::init()->getQuestion("id={$id}&name={$name}&username={$username}&page=");
        $r['pages']=new Page($page,$total,$countOfPage);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        foreach($r['goods_comment'] as &$v){
            $goods_name=$goods->getDetailInfo($v['gopen_id']);
            $v['gopen_id']=$goods_name['name'];
            $r[$v['user_id']]['real_name']=UserMode::init()->getDetail($v['user_id'])['real_name'];
            $r[$v['user_id']]['username']=UserModule::init('user')->getDetail($v['user_id'])['username'];
        }
        $this->cms->tableScene($r,"shop/comment/comment.php");
    }

    function replyTask($id){
        $r['id']=$id;
        $this->cms->formScene($r,"shop/comment/reply.php");
    }

    function replySubmitTask($id,$reply){
        $comment=CommentModule::init('goods_comment');
        $c=$comment->addReply($id,$reply);
        if($c){
            $r['url']=WebRouter::init()->getAction("index");
            $r['message']="回复成功";
        }else{
            $r['message']="回复失败";
        }
        View::displayAsHtml($r,"plugin/state/tips.php");
    }

}